const axios = require("axios");
const baseUrl = "http://localhost:3000/api/";

const queryTestDataByFeature = (feature) => {
  return axios.get(baseUrl + "features/subscenario/" + feature);
};

const queryTestData = (feature) => {
  cy.request('GET', baseUrl + "features/subscenario/" + feature)
    .then(response => {
      testData = response.body.data;
      return testData;
    }).as('testData');
};

const queryTestData2 = (feature) => {
  cy.request('GET', baseUrl + "features/subscenario2/" + feature)
    .then(response => {
      testData = response.body.data;
      return testData;
    }).as('testData');
};

 

// 根据scenario数据生成测试用例
const runTestCaseByScenario = (scenario, delay) => {
  scenario.map((v) => {
    it(v, () => {
      cy.get('@testData').then(testData => {
        console.log('testData-22', testData);
        testData.forEach((item) => {
          if (item.scenario === v) {
            if (delay) {
              // 延迟 执行
              setTimeout(() => {
                cy.runCases(item.cases);
              }, delay);
            } else {
              cy.runCases(item.cases);
            }
          }
        });
      });

    });
  });
};

//解析baseData中的测试用例, 并赋予别名
const parseCsvUser = (environment) => {
  cy.task('parsecsv', 'cypress/fixtures/' + environment + '/baseData.csv').as('csvuser');
};





// 远程获取feature和scenario数据放到json文档中
const generateFeatureData = async () => {
  return new Promise((resolve, reject) => {
    axios.get(baseUrl + "features/scenarios/Map").then(res => {
      const { data } = res;
      // console.log("data---", data);
      resolve(data);
    }).catch(err => {
      reject(err);
    });
  });
};

/**
 * 查询所有的前置操作
 */
const queryPreActions = async()=>{
  return new Promise((resolve, reject) => {
    axios.get(baseUrl + "scenario/preActions/All").then(res => {
      const { data } = res;
      resolve(data);
    }).catch(err => {
      reject(err);
    });
  });
}



module.exports = {
  queryTestDataByFeature,
  queryTestData,
  parseCsvUser,
  generateFeatureData,
  runTestCaseByScenario,
  queryTestData2,
  queryPreActions
};